// src/store/orderStore.js
import { reactive } from "vue";
import { vToast } from "@/plugins/vToast";

// 订单状态映射
export const orderStatusMap = {
  0: "待支付",
  1: "已支付",
  2: "已发货",
  3: "已完成",
  4: "已取消",
  5: "退款中",
  6: "已退款"
};

// 支付方式映射
export const paymentMethodMap = {
  1: "支付宝",
  2: "微信支付",
  3: "银行卡",
  4: "货到付款"
};

// 模拟订单数据
const orders = reactive([
  {
    id: "DD20230701001",
    createTime: 1688205600000,
    customer: {
      id: 1001,
      name: "张三",
      phone: "13800138000"
    },
    products: [
      { id: 1, name: "iPhone 16", price: 5999, quantity: 1, image: "/images/iphone.webp" },
      { id: 2, name: "AirPods Pro", price: 1499, quantity: 1, image: "/images/airpods.webp" }
    ],
    total: 7498,
    status: 1,
    paymentMethod: 2,
    shippingAddress: "北京市朝阳区建国门外大街1号国贸大厦A座1001室",
    note: "请包装精美一些，送人用"
  },
  {
    id: "DD20230701002",
    createTime: 1688209200000,
    customer: {
      id: 1002,
      name: "李四",
      phone: "13900139000"
    },
    products: [
      { id: 3, name: "MacBook Pro 16", price: 14999, quantity: 1, image: "/images/mackbook.jpg" },
      { id: 4, name: "USB-C Hub", price: 299, quantity: 1, image: "/images/chub.webp" }
    ],
    total: 15298,
    status: 2,
    paymentMethod: 1,
    shippingAddress: "上海市浦东新区张江高科技园区龙东大道3000号",
    note: "请周六日配送"
  },
  {
    id: "DD20230702001",
    createTime: 1688295600000,
    customer: {
      id: 1003,
      name: "王五",
      phone: "13700137000"
    },
    products: [
      { id: 5, name: "iPad Pro 2022", price: 6499, quantity: 1, image: "/images/ipad.webp" },
      { id: 6, name: "Apple Pencil", price: 899, quantity: 1, image: "/images/pencil.jpg" }
    ],
    total: 7398,
    status: 0,
    paymentMethod: 3,
    shippingAddress: "广州市天河区珠江新城花城大道68号环球都会广场",
    note: "希望尽快发货"
  }
]);

// 获取所有订单
export const getOrders = () => {
  return [...orders];
};

// 获取订单详情
export const getOrderById = (id) => {
  return orders.find(order => order.id === id);
};

// 更新订单状态
export const updateOrderStatus = (id, newStatus) => {
  const order = orders.find(order => order.id === id);
  if (order) {
    const oldStatus = order.status;
    order.status = newStatus;
    vToast({ 
      message: `订单状态已更新: ${orderStatusMap[oldStatus]} → ${orderStatusMap[newStatus]}`,
      position: "top"
    });
    return true;
  }
  return false;
};

// 取消订单
export const cancelOrder = (id) => {
  const order = orders.find(order => order.id === id);
  if (order) {
    order.status = 4; // 已取消
    vToast({ message: "订单已取消" });
    return true;
  }
  return false;
};

// 添加新订单（模拟）
export const addOrder = (newOrder) => {
  const today = new Date();
  const dateStr = `${today.getFullYear()}${String(today.getMonth() + 1).padStart(2, '0')}${String(today.getDate()).padStart(2, '0')}`;
  const sequence = String(orders.length + 1).padStart(3, '0');
  const id = `DD${dateStr}${sequence}`;
  
  orders.push({
    ...newOrder,
    id,
    createTime: new Date().getTime(),
    status: 0 // 默认待支付状态
  });
  
  return id;
};